//
//  TXHRrettyRuler.m
//  PrettyRuler
//
//  Created by GXY on 15/12/11.
//  Copyright © 2015年 Tangxianhai. All rights reserved.
//  withCount:(NSUInteger)count average:(NSUInteger)average

#import "TXHRrettyRulerOM.h"

#define SHEIGHT 8 // 中间指示器顶部闭合三角形高度
#define INDICATORCOLOR [UIColor redColor].CGColor // 中间指示器颜色

@implementation TXHRrettyRulerOM {
    TXHRulerScrollViewOM * rulerScrollView;
    NSInteger weightMix;
    NSInteger weightMax;

}
@synthesize rulerScrollView;


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        rulerScrollView = [self setUpRulerScrollView];
        rulerScrollView.rulerHeight = frame.size.height;  //刻度尺数字的高度
        rulerScrollView.rulerWidth = frame.size.width;
    }
    return self;
}

- (void)showRulerScrollViewWithMix:(NSUInteger)mix
                               Max:(NSUInteger)max
                      currentValue:(CGFloat)currentValue
                    rulerDirection:(RulerDirectionType)direction
{
    // 最小值、最大值
    rulerScrollView.rulerMixWeight = mix;
    rulerScrollView.rulerMaxWeight = max;
    // 当前值
    rulerScrollView.rulerValue = currentValue;
    
    weightMax = max;
    weightMix = mix;
    
    switch (direction) {
        case RulerDirectionType_Down:
            [rulerScrollView drawRuler];
            break;
        case RulerDirectionType_Up:
            [rulerScrollView drawRulerUp];
            break;
            default:
            break;
    }
    [self addSubview:rulerScrollView];
    [self drawRacAndLine];
}

- (TXHRulerScrollViewOM *)setUpRulerScrollView {
    TXHRulerScrollViewOM * rScrollView = [TXHRulerScrollViewOM new];
    rScrollView.delegate = self;
    rScrollView.showsHorizontalScrollIndicator = NO;
    return rScrollView;
}

#pragma mark - ScrollView Delegate
- (void)scrollViewDidScroll:(TXHRulerScrollViewOM *)scrollView {

    CGFloat offSetX = scrollView.contentOffset.x + self.frame.size.width / 2 - DISTANCELEFTANDRIGHT;
    CGFloat ruleValue = offSetX / DISTANCEVALUE+weightMix;
    if (ruleValue < weightMix) {
        return;
    } else if (ruleValue > weightMax) {
        return;
    }
    if (self.rulerDelegate != nil && [self.rulerDelegate respondsToSelector:@selector(rulerDidScroll:withValue:)]) {
        [self.rulerDelegate rulerDidScroll:self withValue:ruleValue];
    }
}


- (void)scrollViewDidEndDecelerating:(TXHRulerScrollViewOM *)scrollView {
    [self animationRebound:scrollView];
}

- (void)scrollViewDidEndDragging:(TXHRulerScrollViewOM *)scrollView willDecelerate:(BOOL)decelerate {
    [self animationRebound:scrollView];
}

- (void)animationRebound:(TXHRulerScrollViewOM *)scrollView {
    CGFloat offSetX = scrollView.contentOffset.x + self.frame.size.width / 2 - DISTANCELEFTANDRIGHT;
    int index = (offSetX / DISTANCEVALUE) + 0.5;
    CGFloat off_x = index * DISTANCEVALUE + DISTANCELEFTANDRIGHT - self.frame.size.width/2.f;
    [UIView animateWithDuration:.2f animations:^{
        scrollView.contentOffset = CGPointMake(off_x, 0);
    }];
}

- (void)drawRacAndLine {
    // 渐变
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.bounds;
    
    gradient.colors = @[(id)[[UIColor whiteColor] colorWithAlphaComponent:1.f].CGColor,
                        (id)[[UIColor whiteColor] colorWithAlphaComponent:0.0f].CGColor,
                        (id)[[UIColor whiteColor] colorWithAlphaComponent:1.f].CGColor];
    
    gradient.locations = @[[NSNumber numberWithFloat:0.0f],
                           [NSNumber numberWithFloat:0.6f]];
    
    gradient.startPoint = CGPointMake(0, .5);
    gradient.endPoint = CGPointMake(1, .5);
    
    CGMutablePathRef pathArc = CGPathCreateMutable();
    CGPathMoveToPoint(pathArc, NULL, 0, DISTANCETOPANDBOTTOM);
    CGPathAddQuadCurveToPoint(pathArc, NULL, self.frame.size.width / 2, - 20, self.frame.size.width, DISTANCETOPANDBOTTOM);
    //    shapeLayerArc.path = pathArc;
    //    [self.layer addSublayer:shapeLayerArc];
    [self.layer addSublayer:gradient];
    
    // 红色指示器
//    CAShapeLayer *shapeLayerLine = [CAShapeLayer layer];
//    shapeLayerLine.strokeColor = KHexColor(@"#448AFF").CGColor;//[UIColor redColor].CGColor;
//    shapeLayerLine.fillColor = KHexColor(@"#448AFF").CGColor;
//    shapeLayerLine.lineWidth = 1.f;
//    shapeLayerLine.lineCap = kCALineCapSquare;
//    
//    //
//    //
    NSUInteger ruleHeight = 20; // 文字高度
    CGMutablePathRef pathLine = CGPathCreateMutable();
    CGPathMoveToPoint(pathLine, NULL, self.frame.size.width / 2, self.frame.size.height - DISTANCETOPANDBOTTOM - ruleHeight);
    CGPathAddLineToPoint(pathLine, NULL, self.frame.size.width / 2, DISTANCETOPANDBOTTOM + SHEIGHT);
    CGPathAddLineToPoint(pathLine, NULL, self.frame.size.width / 2 - SHEIGHT / 2, DISTANCETOPANDBOTTOM);
    CGPathAddLineToPoint(pathLine, NULL, self.frame.size.width / 2 + SHEIGHT / 2, DISTANCETOPANDBOTTOM);
    CGPathAddLineToPoint(pathLine, NULL, self.frame.size.width / 2, DISTANCETOPANDBOTTOM + SHEIGHT);
//
//    shapeLayerLine.path = pathLine;
//    [self.layer addSublayer:shapeLayerLine];
}
#pragma mark - tool method
- (CGFloat)notRounding:(CGFloat)price afterPoint:(NSInteger)position {
    
    NSDecimalNumberHandler*roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    NSDecimalNumber*ouncesDecimal;
    NSDecimalNumber*roundedOunces;
    ouncesDecimal = [[NSDecimalNumber alloc]initWithFloat:price];
    roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
    return [roundedOunces floatValue];
}

- (BOOL)valueIsInteger:(NSNumber *)number {
    NSString *value = [NSString stringWithFormat:@"%f",[number floatValue]];
    if (value != nil) {
        NSString *valueEnd = [[value componentsSeparatedByString:@"."] objectAtIndex:1];
        NSString *temp = nil;
        for(int i =0; i < [valueEnd length]; i++)
        {
            temp = [valueEnd substringWithRange:NSMakeRange(i, 1)];
            if (![temp isEqualToString:@"0"]) {
                return NO;
            }
        }
    }
    return YES;
}
@end

